package com.ktgj.customer.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ktgj.common.core.constant.Constant;
import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.customer.domain.Employee;
import com.ktgj.customer.domain.MenuDTO;
import com.ktgj.customer.domain.dto.SysMenuAllDTO;
import com.ktgj.customer.domain.dto.SysMenuLoginDTO;
import com.ktgj.customer.service.IEmployeeService;
import com.ktgj.customer.service.IPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.MenuMapper;
import com.ktgj.customer.domain.Menu;
import com.ktgj.customer.service.IMenuService;

/**
 * 菜单Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-10-15
 */
@Service
public class MenuServiceImpl implements IMenuService 
{
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private IPositionService positionService;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询菜单
     * 
     * @param menuId 菜单ID
     * @return 菜单
     */
    @Override
    public MenuDTO selectMenuById(Long menuId)
    {
        return menuMapper.selectMenuById(menuId);
    }

    /**
     * 查询菜单列表
     * 
     * @param menu 菜单
     * @return 菜单
     */
    @Override
    public List<MenuDTO> selectMenuList(MenuDTO menu)
    {
        return menuMapper.selectMenuList(menu);
    }

    /**
     * 新增菜单
     * 
     * @param menu 菜单
     * @return 结果
     */
    @Override
    public int insertMenu(MenuDTO menu)
    {
        return menuMapper.insertMenu(menu);
    }

    /**
     * 修改菜单
     * 
     * @param menu 菜单
     * @return 结果
     */
    @Override
    public int updateMenu(MenuDTO menu)
    {
        return menuMapper.updateMenu(menu);
    }

    /**
     * 批量删除菜单
     * 
     * @param menuIds 需要删除的菜单ID
     * @return 结果
     */
    @Override
    public int deleteMenuByIds(Long[] menuIds)
    {
        return menuMapper.deleteMenuByIds(menuIds);
    }

    /**
     * 删除菜单信息
     * @param menuId 菜单ID
     * @return 结果
     */
    @Override
    public int deleteMenuById(Long menuId)
    {
        return menuMapper.deleteMenuById(menuId);
    }

    @Override
    public List<MenuDTO> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<MenuDTO> menuList = queryListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }
        List<MenuDTO> userMenuList = new ArrayList<>();
        for (MenuDTO menu : menuList) {
            if (menuIdList.contains(menu.getMenuId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<MenuDTO> queryListParentId(Long parentId) {
        return menuMapper.queryListParentId(parentId);
    }

    @Override
    public List<MenuDTO> getUserMenuList(Long userId) {
        //系统管理员拥有最高权限
        if (userId == Constant.SUPER_ADMIN) {
            return getAllMenuList(null);
        }
        //用户菜单列表
        Employee employee = employeeService.selectEmployeeById(userId);
        List<Long> menuIdList = positionService.queryAllMenuId(employee.getPositionId());
        return getAllMenuList(menuIdList);
    }

    /**
     * 获取所有菜单列表
     * @param menuIdList
     * @return
     */
    private List<MenuDTO> getAllMenuList(List<Long> menuIdList) {
        List<MenuDTO> menuList = queryListParentId(0L, menuIdList);
        getMenuTreeList(menuList, menuIdList);
        return menuList;
    }

    /**
     * 递归
     * @param menuList
     * @param menuIdList
     * @return
     */
    private List<MenuDTO> getMenuTreeList(List<MenuDTO> menuList, List<Long> menuIdList) {
        List<MenuDTO> subMenuList = new ArrayList<>();
        for(MenuDTO entity : menuList){
            entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
            subMenuList.add(entity);
        }
        return subMenuList;
    }

    @Override
    public List<MenuDTO> menuListTree() {
        List<MenuDTO> menuDTOS = menuMapper.selectMenuList(null);
        List<MenuDTO> entityMenu =menuDTOS.stream().filter(menuDTO ->
            menuDTO.getMenuParent() == 0
        ).map((menu) -> {
            menu.setList(getMenu(menu, menuDTOS));
            return menu;
        }).collect(Collectors.toList());
        return entityMenu;
    }

    @Override
    public Map<String, List<SysMenuLoginDTO>> selectMenusByPositionId(Long positionId, String model) {
        Map<String, List<SysMenuLoginDTO>> map = new HashMap<>();
        List<SysMenuLoginDTO> menusAll = menuMapper.selectMenusByPositionId(positionId, model);

        if (menusAll.size() > 0){
            List<SysMenuLoginDTO> menus = menuUtil(menusAll);
            if (menus.size() > 0){

                map.put("ch", menus);
            }
        }
        return map;
    }

    @Override
    public List<SysMenuLoginDTO> queryMenuAll() {
        List<SysMenuLoginDTO> all = menuMapper.selectMenuAll();
        for (SysMenuLoginDTO item : all) {
            List<SysMenuLoginDTO> menus = menuUtil(item.getCh());
            item.setMenuId("0");

            switch (item.getMenuName()){
                case "system":
                    item.setMenuName("系统");
                    break;
                case "customer":
                    item.setMenuName("会员");
                    break;
                case "flight":
                    item.setMenuName("机票");
                    break;
                case "VIPBoarding":
                    item.setMenuName("值机");
                    break;
                case "vipHall":
                    item.setMenuName("贵宾厅");
                    break;
                case "VIPChannel":
                    item.setMenuName("vip通道");
                    break;
                case "airportService":
                    item.setMenuName("机场服务");
                    break;
                case "flightDelay":
                    item.setMenuName("航班延误");
                    break;
                case "train":
                    item.setMenuName("火车票");
                    break;
                case "hotel":
                    item.setMenuName("酒店");
                    break;
                case "travel":
                    item.setMenuName("旅游");
                    break;
                case "ticket":
                    item.setMenuName("门票");
                    break;
                case "specialCar":
                    item.setMenuName("专车");
                    break;
                case "parking":
                    item.setMenuName("代泊车");
                    break;
                case "DDDriving":
                    item.setMenuName("滴滴代驾");
                    break;
                case "mall":
                    item.setMenuName("商城");
                    break;
                case "healthy":
                    item.setMenuName("健康");
                    break;
                case "insurance":
                    item.setMenuName("保险");
                    break;
                case "finance":
                    item.setMenuName("财务");
                    break;
            }
            item.setCh(menus);
        }
        return all;
    }


    //递归遍历菜单
    public List<MenuDTO> getMenu(MenuDTO root, List<MenuDTO> all) {
        List<MenuDTO> children =all.stream().filter(menuDTO -> {
            return menuDTO.getMenuParent() == root.getMenuId();
        }).map(menuDTO -> {
            menuDTO.setList(getMenu(menuDTO, all));
            return menuDTO;
        }).collect(Collectors.toList());
        return children;
    }

    public List<SysMenuLoginDTO> menuUtil(List<SysMenuLoginDTO> menusAll) {
        List<SysMenuLoginDTO> menus = new ArrayList<>();
        for (int i = menusAll.size() - 1; i >= 0; i--)
            if (menusAll.get(i).getParentId() == null || menusAll.get(i).getParentId().equals("") || menusAll.get(i).getParentId().equals("0")) {
                menus.add(menusAll.get(i));
                menusAll.remove(i);//删除元素减轻循环压力
            }
        for (int j = menus.size() - 1; j >= 0; j--) {
            menus.get(j).setCh(new ArrayList<>());
            for (int i = menusAll.size() - 1; i >= 0; i--) {
                menusAll.get(i).setCh(new ArrayList<>());
                if (menus.get(j).getMenuId().equals(menusAll.get(i).getParentId())) {
                    menus.get(j).getCh().add(menusAll.get(i));
                    menusAll.remove(i);//删除元素减轻循环压力
                }
            }
        }
        return menus;
    }
}
